/**
 * 
 */
package edu.isistan.genetic.operators;

import java.util.Vector;
import edu.isistan.metamodel.*;

/**
 * @author mtrotti
 *
 */
public class Encapsulate implements Operator {
	
	protected int maxNestedEncapsulations;
	
	public Encapsulate(){
		this.maxNestedEncapsulations = 1;
	}
	
	public Encapsulate(int aMaxNestedEncapsulations){
		this.maxNestedEncapsulations = aMaxNestedEncapsulations;
	}
	
	private int getNestedEncapsulationsDeep(DataType aDataType){
		if (aDataType.isCompoused() && !aDataType.isArray()){
			Composed aComposed= (Composed)aDataType;
			int max=0;
			for (DataType dataType : aComposed.getDataTypes()) {
				if (dataType.isCompoused()){
					int part=  this.getNestedEncapsulationsDeep(dataType);
					if (max< part)
						max=part;
				}
			}
			return (max + 1);
		}
		return 0;
	}
	/* (non-Javadoc)
	 * @see Operators.Operator#isCompatible(edu.isistan.metamodel.DataType)
	 */
	
	public boolean isCompatible(DataType aDataType) {
		return (this.getNestedEncapsulationsDeep(aDataType) <= this.maxNestedEncapsulations);
	}
	
	
	public boolean isEncapsulationCompatible(){
		return true;
	}
	
	
	public Vector<DataType> mutate(Vector<DataType> aDataTypes) {
		Vector<DataType> aResultSet = new Vector<DataType>();
		StringBuffer sb=new StringBuffer();
		Vector<String> aFieldnames = new Vector<String>();
		for (DataType dataType : aDataTypes) {
			sb.append(dataType.getName() + "_");
			aFieldnames.add(dataType.getName());
			
		}
		sb.append("Object");
		aResultSet.add(new Composed(sb.toString(), "Composed", aDataTypes, aFieldnames, "NO DOC"));
		return aResultSet;
	}

	@Override
	public String toString() {
		return "Encapsulate Max Nested Encapsulations="	+ maxNestedEncapsulations;
	}

	
}
